package myPackage;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.util.Locale;


public class PixelGrabberTest {
    
    public PixelGrabberTest() {
    }
    
    public static void processImage(String inFile) {
        
        Image image = Toolkit.getDefaultToolkit().getImage(inFile);
        
        System.out.println(Locale.getDefault().getDisplayName());
        
        try {
        	            PixelGrabber grabber = 
                    new PixelGrabber(image, 0, 0, -1, -1, false);
            
            if (grabber.grabPixels()) {          
                int width = grabber.getWidth();
                int height = grabber.getHeight();
                
                if (isGreyscaleImage(grabber)) {
                    byte[] data = (byte[]) grabber.getPixels();
                    
                    // Process greyscale image ...
                    int i;
                    for (i=0;i<width*height;i++){
                    	System.out.println(data[i]);
                    	}
                    
                }
                else {
                    int[] data = (int[]) grabber.getPixels();
                    
                    // Process Color image
                    int i;
                    int j=0;
                    int k=0;
                    int l=0;
                    int m=0;
                    int [] x = new int [1000];
                    int [] y = new int [1000];
                    
                    int array2d[][] = new int [1000][1000];
                    //convert data array to 2d
                                 
                    	for (i=0;i<height;i++){
                    		for (j=width;j>0;j--){
                    			//System.out.println(data[x]);
									array2d[i][j] = data[k];
									k++;
									if (isThisBlack(array2d[i][j])){
                					
                					x[l] = i;
                					y[m] = j;
                					System.out.print(x[l]);	
                					System.out.print(",");
                					System.out.println(y[m]);
                					l++; m++;
                					}
                    			}
                    		}
                                                    
                	}

            	}
        }
                    
  
        
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
    
    public static final boolean isGreyscaleImage(PixelGrabber pg) {
        return pg.getPixels() instanceof byte[];
    }
    
    public static boolean isThisBlack(int pixel) {
    	int alpha = (pixel >> 24) & 0xff;
    	int red   = (pixel >> 16) & 0xff;
    	int green = (pixel >>  8) & 0xff;
    	int blue  = (pixel      ) & 0xff;
    	if(blue<15 && green <15 && red <15)
    	{
    		return true;
    	}else if(blue<230 || red <230 || green <230)
    	{
    		System.out.println("Got non b/w color : "+red+","+blue+","+green);
    	}
    	return false;
     }

    
    public static void main(String args[]) {
        
    	String inFile = "line1.jpg";
    	
    	File file = new File(inFile);
    	if(file.exists())
    		System.out.println("File exists");
    	
    	processImage(inFile);
          
    }
} 